#include "configwidget_pg.h"

#include <iostream>

namespace verema
{

ConfigWidget_Pg::ConfigWidget_Pg(QWidget *cw)
: vbl_mainlayout(this)
, grl_top_layout()
, grb_groupbox("Objektdefinitionen", this)
, grl_groupbox_layout(&grb_groupbox)
, lbl_language("Sprache", this)
, lbl_defaultview("Ansicht", this)
, cmb_language(this)
, cmb_defaultview(this)
, lbl_unit("Unit", &grb_groupbox)
, led_unit(&grb_groupbox)
, lbl_subunit("Subunit", &grb_groupbox)
, led_subunit(&grb_groupbox)
, lbl_item("Item", &grb_groupbox)
, led_item(&grb_groupbox)
, btn_cancel("Abbrechen",this)
, btn_save("Speichern",this)
, configWidget(cw)
{
    cmb_language.addItem("Deutsch");

    cmb_defaultview.addItem("DataView");
	cmb_defaultview.addItem("VisualView");

    grl_top_layout.addWidget(&lbl_language, 0, 0);
    grl_top_layout.addWidget(&cmb_language, 0, 1);
    grl_top_layout.addWidget(&lbl_defaultview, 1, 0);
    grl_top_layout.addWidget(&cmb_defaultview, 1, 1);

    grl_groupbox_layout.addWidget(&lbl_unit, 0, 0, 1, 1);
    grl_groupbox_layout.addWidget(&led_unit, 0, 1, 1, 1);
    grl_groupbox_layout.addWidget(&lbl_subunit, 1, 0, 1, 1);
    grl_groupbox_layout.addWidget(&led_subunit, 1, 1, 1, 1);
    grl_groupbox_layout.addWidget(&lbl_item, 2, 0, 1, 1);
    grl_groupbox_layout.addWidget(&led_item, 2, 1, 1, 1);

    grb_groupbox.setLayout(&grl_groupbox_layout);

    hbl_button_layout.addWidget(&btn_save);
    hbl_button_layout.addWidget(&btn_cancel);

    vbl_mainlayout.addLayout(&grl_top_layout);
    vbl_mainlayout.addWidget(&grb_groupbox);
    vbl_mainlayout.addLayout(&hbl_button_layout);

    setLayout(&vbl_mainlayout);

    //Connections
	connect(&cmb_defaultview, SIGNAL(currentIndexChanged(int)), this, SLOT(definitionChange()));
	connect(&led_unit, SIGNAL(textChanged(QString)), this, SLOT(definitionChange()));
	connect(&led_subunit, SIGNAL(textChanged(QString)), this, SLOT(definitionChange()));
	connect(&led_item, SIGNAL(textChanged(QString)), this, SLOT(definitionChange()));
	connect(&btn_save, SIGNAL(clicked()), this, SLOT(saveDefinitions()));
	connect(&btn_cancel, SIGNAL(clicked()), this, SLOT(close()));

	btn_save.setDisabled(true);
}

void ConfigWidget_Pg::definitionChange()
{
	btn_save.setEnabled(true);
}

void ConfigWidget_Pg::loadData()
{
	Config* m_config = Config::getInstance();
	if(m_config->view() == "DataView")
	{
	    cmb_defaultview.setCurrentIndex(0);
	}
	else
	{
	    cmb_defaultview.setCurrentIndex(1);
	}

    led_unit.setText(m_config->unit().c_str());
    led_subunit.setText(m_config->subunit().c_str());
    led_item.setText(m_config->item().c_str());

    btn_save.setEnabled(false);
}

void ConfigWidget_Pg::saveDefinitions()
{
	Config* m_config = Config::getInstance();

	if(cmb_defaultview.currentIndex() == 0)
	{
	    m_config->view("DataView");
	}
	else
	{
	    m_config->view("VisualView");
	}

	m_config->unit(led_unit.text().toStdString());
	m_config->subunit(led_subunit.text().toStdString());
	m_config->item(led_item.text().toStdString());

	btn_save.setEnabled(false);
    emit configChanged();
}

void ConfigWidget_Pg::close()
{
    configWidget->hide();
}

} // namespace verema
